package cn.lee.cplibrary.widget.imageview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.appcompat.widget.AppCompatImageView;

import cn.lee.cplibrary.R;

/**
 * 圆角矩形ImageView
 */
public class RoundImageView extends AppCompatImageView {
    private static final int BODER_RADIUS_DEFAULT = 8;
    private final RectF roundRect = new RectF();
    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();
    private float borderRadius = 8;//圆角半径
    // 左上角是否为圆角
    private boolean corners_top_left = true;

    // 右上角是否为圆角
    private boolean corners_top_right = true;

    // 左下角是否为圆角
    private boolean corners_bottom_left = true;

    // 右下角是否为圆角
    private boolean corners_bottom_right = true;

    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
//        float density = getResources().getDisplayMetrics().density;
//        borderRadius = borderRadius * density;
        //属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.cp_RoundImageView);
        corners_top_left = array.getBoolean(R.styleable.cp_RoundImageView_cp_top_left, true);
        corners_top_right = array.getBoolean(R.styleable.cp_RoundImageView_cp_top_right, true);
        corners_bottom_left = array.getBoolean(R.styleable.cp_RoundImageView_cp_bottom_left, true);
        corners_bottom_right = array.getBoolean(R.styleable.cp_RoundImageView_cp_bottom_right, true);
        borderRadius = array.getDimensionPixelSize(R.styleable.cp_RoundImageView_cp_borderRadius,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BODER_RADIUS_DEFAULT,
                        getResources().getDisplayMetrics()));
        array.recycle();
    }

    /**
     * 设置圆角半径
     *
     * @param borderRadius 半径，单位dp
     */
    public void setBorderRadius(int borderRadius) {
        this.borderRadius = dp2px(borderRadius);
        invalidate();
    }
    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int w = getWidth();
        int h = getHeight();
        roundRect.set(0, 0, w, h);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG) ;
        canvas.drawRoundRect(roundRect, borderRadius, borderRadius, zonePaint);
        //隐去不需要的角度
        if (!corners_top_left) {
            canvas.drawRect(0, 0, borderRadius, borderRadius, zonePaint);
        }
        if (!corners_top_right) {
            canvas.drawRect(roundRect.right - borderRadius, 0, roundRect.right, borderRadius, zonePaint);
        }
        if (!corners_bottom_left) {
            canvas.drawRect(0, roundRect.bottom - borderRadius, borderRadius, roundRect.bottom, zonePaint);
        }
        if (!corners_bottom_right) {
            canvas.drawRect(roundRect.right - borderRadius, roundRect.bottom - borderRadius, roundRect.right, roundRect.bottom, zonePaint);
        }
        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
        super.draw(canvas);
        canvas.restore();
    }

    private int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics());
    }
}